package com.example.storage.service.impl;

import com.example.storage.service.StorageService;
import io.seata.core.context.RootContext;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class StorageServiceImpl implements StorageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StorageService.class);

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public int deduct(String commodityCode, int count) {
        LOGGER.info("Stock Service Begin ... xid: {}", RootContext.getXID());
        LOGGER.info("Deducting inventory SQL: update stock_tbl set count = count - {} where commodity_code = {}", count,
                commodityCode);
        int result = jdbcTemplate.update("update stock_tbl set count = count - ? where commodity_code = ?", count, commodityCode);
        LOGGER.info("Stock Service End ... ");
        return result;
    }
}
